<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 皮肤配置组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Themes_Config extends Widget_Abstract_Options
{
    /**
     * 绑定动作
     *
     * @access public
     * @return void
     * @throws Widget_Exception
     */
    public function execute()
    {
        $this->user->pass('admin');
        if (!self::isExists()) {
            throw new Widget_Exception(_t('外观配置功能不存在'), 404);
        }
    }

    /**
     * 配置功能是否存在
     * 
     * @access public
     * @return boolean
     */
    public static function isExists()
    {
        $options = Cola::widget('Widget_Options');
        $configFile = $options->themeFile($options->theme, 'functions.php');
  
        if (file_exists($configFile)) {
            require_once $configFile;
            
            if (function_exists('themeConfig')) {
                return true;
            }
        }
        
        return false;
    }

    /**
     * 配置外观
     *
     * @access public
     * @return Form
     */
    public function form()
    {
        $form = new Form($this->security->actionUrl('themes-edit?config', false));
        themeConfig($form);
        $inputs = $form->getInputs();
        
        if (!empty($inputs)) {
            foreach ($inputs as $key => $val) {
                $form->getInput($key)->value($this->options->{$key});
            }
        }

        $submit = new Form_Submit(NULL, NULL, _t('保存设置'));
        $form->set($submit);
        return $form;
    }
}
